<template>
  <a-drawer
    width="600px"
    :visible="visible"
    :bodyStyle="{ padding: '1px' }"
    :title="title"
    @close="cancel"
  >
    <div class="eip-drawer-body beauty-scroll">
      <a-spin :spinning="spinning">
        <a-form-model
          ref="form"
          :model="form"
          :rules="rules"
          :label-col="config.labelCol"
          :wrapper-col="config.wrapperCol"
        >
          <a-row>
            <a-col>
              <a-form-model-item label="Id" prop="config">
                <a-input v-model="form.configId" placeholder="主键自动生成" />
              </a-form-model-item>
              <a-form-model-item label="名称" prop="name">
                <a-input
                  v-model="form.name"
                  placeholder="请输入名称"
                  allow-clear
                />
              </a-form-model-item>
              <a-form-model-item label="数据来源" prop="dataFrom">
                <a-select
                  prop="dataFrom"
                  @change="dataFromChange"
                  placeholder="请选择数据来源"
                  v-model="form.dataFrom"
                >
                  <a-select-option :value="0">表</a-select-option>
                  <a-select-option :value="1">视图</a-select-option>
                  <a-select-option :value="2">存储过程</a-select-option>
                </a-select>
              </a-form-model-item>

              <a-form-model-item :label="dataFrom.name" prop="dataFromName">
                <a-select
                  allow-clear
                  placeholder="请选择数据来源"
                  show-search
                  :filter-option="filterOption"
                  v-model="form.dataFromName"
                >
                  <a-select-option
                    v-for="(item, i) in dataFrom.source"
                    :key="i"
                    :value="item.name"
                  >
                    {{ item.name }} {{ item.description }}
                  </a-select-option>
                </a-select>
              </a-form-model-item>
              <a-form-model-item label="编辑页面" prop="editConfigId">
                <a-select
                  allow-clear
                  placeholder="请选择编辑页面"
                  v-model="form.editConfigId"
                >
                  <a-select-option
                    v-for="(item, i) in editConfig"
                    :key="i"
                    :value="item.configId"
                  >
                    {{ item.name }}
                  </a-select-option>
                </a-select>
              </a-form-model-item>
              <a-form-model-item label="卡片列表" prop="cardConfigId">
                <a-select
                  allow-clear
                  placeholder="请选择卡片列表"
                  v-model="form.cardConfigId"
                >
                  <a-select-option
                    v-for="(item, i) in cardConfig"
                    :key="i"
                    :value="item.configId"
                  >
                    {{ item.name }}
                  </a-select-option>
                </a-select>
              </a-form-model-item>
              <a-form-model-item label="排序号" prop="orderNo">
                <a-input-number
                  id="inputNumber"
                  style="width: 100%"
                  placeholder="请输入排序号"
                  v-model="form.orderNo"
                  :min="0"
                  :max="999"
                />
              </a-form-model-item>
              <a-form-model-item label="禁用" prop="isFreeze">
                <a-switch v-model="form.isFreeze" />
              </a-form-model-item>
              <a-form-model-item label="备注" prop="remark">
                <a-input
                  allow-clear
                  v-model="form.remark"
                  type="textarea"
                  placeholder="请输入备注"
                />
              </a-form-model-item>
            </a-col>
          </a-row>
        </a-form-model>
      </a-spin>
    </div>
    <div class="eip-drawer-toolbar flex justify-between align-center">
      <div>
        <a-checkbox v-if="!configId" v-model="save.retain">
          继续创建时，保留本次提交内容
        </a-checkbox>
      </div>

      <a-space>
        <a-button
          v-if="!configId"
          key="submit"
          @click="saveContinue"
          :loading="loading"
          ><a-icon type="save" />提交并继续创建</a-button
        >
        <a-button
          key="submit"
          @click="saveClose"
          type="primary"
          :loading="loading"
          ><a-icon type="save" />提交</a-button
        >
      </a-space>
    </div>
  </a-drawer>
</template>

<script>
import {
  query,
  table,
  view,
  proc,
  save,
  findById,
} from "@/services/system/agile/list/edit";
import { newGuid } from "@/utils/util";
export default {
  name: "edit",
  data() {
    return {
      config: {
        labelCol: { span: 4 },
        wrapperCol: { span: 18 },
      },
      form: {
        configId: newGuid(),
        dataFrom: 0,
        dataFromName: [],
        name: null,
        orderNo: 1,
        configType: 1,
        type: undefined,
        isFreeze: false,
        remark: null,
        editConfigId: undefined,
        cardConfigId: undefined,
      },
      dataFrom: {
        name: "表",
        source: [],
      },
      rules: {
        name: [
          {
            required: true,
            message: "请输入名称",
            trigger: "blur",
          },
        ],
      },

      loading: false,
      spinning: false,
      save: {
        continue: false,
        retain: false,
      },
      editConfig: [],
      cardConfig: [],
    };
  },

  props: {
    visible: {
      type: Boolean,
      default: false,
    },
    data: {
      type: Array,
    },
    configId: {
      type: String,
    },
    copy: {
      type: Boolean,
      default: false,
    },
    title: {
      type: String,
    },
  },
  mounted() {
    this.find();
  },
  methods: {
    /**
     *
     * @param {搜索} input
     * @param {*} option
     */
    filterOption(input, option) {
      return (
        option.componentOptions.children[0].text
          .toLowerCase()
          .indexOf(input.toLowerCase()) >= 0
      );
    },

    /**
     * 数据来源改变
     */
    dataFromChange(e) {
      this.form.dataFromName = [];
      this.initDataFrom();
    },

    /**
     * 初始化数据来源
     */
    initDataFrom() {
      switch (this.form.dataFrom) {
        case 0:
          this.dataFrom.name = "表";
          this.initTable();
          break;
        case 1:
          this.dataFrom.name = "视图";
          this.initView();
          break;
        case 2:
          this.dataFrom.name = "存储过程";
          this.initProc();
          break;
      }
    },

    /**
     * 初始化表
     */
    initTable() {
      let that = this;
      table().then((result) => {
        that.dataFrom.source = result.data;
      });
    },

    /**
     * 初始化存储过程
     */
    initProc() {
      let that = this;
      proc().then((result) => {
        that.dataFrom.source = result.data;
      });
    },

    /**
     * 初始化视图
     */
    initView() {
      let that = this;
      view().then((result) => {
        that.dataFrom.source = result.data;
      });
    },

    /**
     * 取消
     */
    cancel() {
      this.$emit("update:visible", false);
    },

    /**
     * 保存
     */
    saveComfirm() {
      let that = this;
      this.$refs.form.validate((valid) => {
        if (valid) {
          that.loading = true;
          that.$loading.show({ text: that.eipMsg.saveloading });
          that.form.configId = that.copy ? newGuid() : that.form.configId;
          table(that.form).then((result) => {
            if (result.code === that.eipResultCode.success) {
              save(that.form).then(function (result) {
                if (result.code === that.eipResultCode.success) {
                  that.$message.success(result.msg);
                  if (that.save.continue) {
                    //不保留上次内容
                    if (!that.save.retain) {
                      that.$refs.form.resetFields();
                    }
                    that.form.configId = newGuid();
                  } else {
                    that.cancel();
                  }
                  that.$emit("ok", result);
                } else {
                  that.$message.error(result.msg);
                }
                that.loading = false;
                that.$loading.hide();
              });
            } else {
              that.$message.error(result.msg);
              that.loading = false;
              that.$loading.hide();
            }
          });
        } else {
          return false;
        }
      });
    },
    /**
     *
     */
    saveContinue() {
      this.save.continue = true;
      this.saveComfirm();
    },

    /**
     *
     */
    saveClose() {
      this.save.continue = false;
      this.saveComfirm();
    },
    /**
     * 初始化编辑配置
     */
    initEditConfig() {
      let that = this;
      query({ size: 99999, configType: 2 }).then(function (result) {
        that.editConfig = result.data;
      });
    },

    /**
     * 初始化卡片配置
     */
    initCardConfig() {
      let that = this;
      query({ size: 99999, configType: 3 }).then(function (result) {
        that.cardConfig = result.data;
      });
    },

    /**
     * 根据Id查找
     */
    find() {
      let that = this;
      if (this.configId) {
        that.spinning = true;
        findById(this.configId).then(function (result) {
          let form = result.data;
          that.form.configId = form.configId;
          that.form.dataFromName = form.dataFromName;
          that.form.dataFrom = form.dataFrom;
          that.form.name = form.name;
          that.form.orderNo = form.orderNo;
          if (that.copy) {
            that.form.orderNo += 1;
          }
          that.form.isFreeze = form.isFreeze;
          that.form.remark = form.remark;
          that.form.type = form.type;
          that.initDataFrom();
          that.initEditConfig();
          that.initCardConfig();

          that.form.editConfigId = form.editConfigId;
          that.form.cardConfigId = form.cardConfigId;
          that.spinning = false;
        });
      } else {
        that.initTable();
        that.initEditConfig();
        that.initCardConfig();
      }
    },
  },
};
</script>

<style lang="less" scoped></style>
